package com.itmayiedu.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import com.itmayiedu.annotation.MyConstraint;
import com.itmayiedu.entity.User;
import com.itmayiedu.service.UserService;

public class MyConstraintValidator implements ConstraintValidator<MyConstraint, String> {

	/*ConstraintValidator<A, T>
	参数一：验证的注解
	参数二：验证的类型
	ConstraintValidator<MyConstraint, String> 当前注解只能放在String类型字段上才会起作用
	*/
	@Autowired
	private UserService userService;
	
	@Override
	public void initialize(MyConstraint constraintAnnotation) {
		System.out.println("自定义验证注解 MyConstraintValidator.");
	}

	@Override
	public boolean isValid(String value, ConstraintValidatorContext context) {
		//User user = userService.getUser("1");
		if(StringUtils.isEmpty(value)) {
			return false;
		}
		System.out.println("MyConstraintValidator.: " + value);
		return true;//false：校验失败；true：校验成功
	}
	
}
